<?php
namespace app\admin\controller\system;
use app\admin\addons\Addons;
use app\admin\addons\FieldHand;
use app\admin\builder\MBuilder;
use app\admin\model\publicuse\PublicUse;
use app\common\controller\AdminController;

/**
 * @ControllerAnnotation(title="应用管理")
 * Class Myapp
 * @package app\admin\controller\system
 */
class Myapp extends AdminController
{
//    use \app\admin\traits\Curd;
    /**
     * @NodeAnotation(title="列表")
     */
    public function index(){
        if($this->request->isAjax()){
            $array = PublicUse::getdir(root_path().'addons'.DIRECTORY_SEPARATOR,['system']);
            $newdata = [];
            $datas = $this->request->param();

            foreach ($array as $key=>$value){
                $da = root_path().'addons'.DIRECTORY_SEPARATOR.$value.DIRECTORY_SEPARATOR.'info.ini';
                if(!is_file($da)){
                    continue;
                }
                $value = parse_ini_file($da);
                $value['state'] = intval($value['state']);
//                dump($value);die();
                $newdata[] = $value;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => count($newdata),
                'data'  => $newdata,
            ];
//            echo "<pre>";
//            print_r($data);
//            exit;
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 添加插件
     */
    public function add(){
        $array = PublicUse::getdir(root_path().'addons'.DIRECTORY_SEPARATOR,['.svn','layout']);
        $data = $this->request->param();
        if($this->request->isAjax()){
            if(empty($data['name'])){
                return $this->error('没有输入标识');
            }
            list($bool,$msg) = FieldHand::CreateAddons($data);
            if(!$bool){
                return $this->error($msg);
            }
            //生成ini
            return $this->success('添加成功');
        }else{
            return MBuilder::make()
                ->setAction()
                ->setInput('插件名','title','')
                ->setInput('插件标识','name','','不可与其他的标识冲突')
                ->setTextarea('描述','intro','')
                ->setInput('作者姓名','author','')
                ->setCheckbox('依赖插件','relyon',$array)
                ->setSubmitButton()
                ->setResetButton()
                ->fetch();
        }
    }
    public function install(){
        $name = $this->request->param('name');
        list($bool,$msg) = Addons::InstallAddonss($name);
        if(!$bool){
            return $this->error($msg);
        }
        return $this->success('安装成功');
    }
    public function modify(){
        return $this->error('暂时不支持');
    }
}